<?php

namespace App\Http\Controllers\H5;

use App\Models\Shop;
use Illuminate\Http\Request;

class KefuController extends BaseController
{
    public function index()
    {
        return $this->view('h5.kefu.index');
    }

    public function bind($shop_id)
    {
        $shop = Shop::find($shop_id);
        $kefu = $shop->kefus()->where('uid', $this->uid)->first();
        return $this->view('h5.kefu.bind', compact('shop_id', 'shop', 'kefu'));
    }

    public function bindUser(Request $request)
    {
        $shop_id = $request->input('shop_id');
        $shop = Shop::find($shop_id);
        $kefu = [];
        if ($shop){
            if (!$shop->kefus()->where('uid', $this->uid)->exists()){
                $kefu = $shop->kefus()->create([
                    'uid'=>$this->uid,
                    'name'=>$this->username,
                    'openid'=>$this->oauthUser->openid()
                ]);
            }
        }
        return ajaxReturn(['kefu'=>$kefu]);
    }
}
